package com.ruicar.afs.cloud.batch.dayend.handle.run;

import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.core.uid.UidGenerator;
import com.ruicar.afs.cloud.message.sendmessage.config.MessageConfig;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author infinity
 * @version 1.0
 * @date 2020/9/23 18:07
 */
@AllArgsConstructor
@Component
public class MessageProcessor {
    private final UidGenerator uidGenerator;
    private final MessageConfig messageConfig;

    //保存
    public void saveSms(PreparedStatement ps, Map map, String phone, String contractNo, String name, Map valueMap, java.sql.Date batchDate) throws SQLException {

        String templateContent = (String) map.get( "templateContent" );
        //替换模板值
        templateContent = templateContent
                .replace( "${custName}", name )
                .replace( "${number}", phone )
                .replace( "${contractNo}", contractNo )
                .replace( "${day}", valueMap.get( "day" ) != null ? (String) valueMap.get( "day" ) : "" )
                .replace( "${time}", valueMap.get( "time" ) != null ? (String) valueMap.get( "time" ) : "" )
                .replace( "${amt}", valueMap.get( "amt" ) != null ? (String) valueMap.get( "amt" ) : "" )
                .replace( "${bankNo}", valueMap.get( "bankNo" ) != null ? (String) valueMap.get( "bankNo" ) : "" );
        ps.setLong( 1, uidGenerator.getUID() );
        ps.setString( 2, contractNo );
        ps.setString( 3, (String) map.get( "channel" ) );
        ps.setString( 4, messageConfig.getCode() );
        ps.setString( 5, messageConfig.getUid() );
        ps.setString( 6, phone );
        ps.setString( 7, (String) map.get( "templateId" ) );
        ps.setString( 8, null );
        ps.setString( 9, templateContent );
        ps.setString( 10, (String) map.get( "priority" ) );
        ps.setString( 11, messageConfig.getOrigin() );
        ps.setString( 12, messageConfig.getDeptId() );
        ps.setString( 13, "system" );
        ps.setDate( 14, batchDate );
        ps.addBatch();
    }

    //获取短信信息
    public Map getSms(Connection contractManage, String templateType, String templateBusiness, String status) throws SQLException {
        StringBuffer sqlSms = new StringBuffer();
        sqlSms.append( "select sms.template_content, sms.channel,sms.priority,sms.template_id  " );
        sqlSms.append( " from manage_sms_template sms " );
        sqlSms.append( " where sms.template_type=? and  template_business =?  and status=?" );
        PreparedStatement psSms = null;
        ResultSet rsSms = null;
        Map map = new HashMap();
        psSms = contractManage.prepareStatement( sqlSms.toString() );
        psSms.setString( 1, templateType );
        psSms.setString( 2, templateBusiness );
        psSms.setString( 3, status );
        rsSms = psSms.executeQuery();
        if (rsSms != null && rsSms.next()) {
            map.put( "templateContent", rsSms.getString( "template_content" ) );
            map.put( "channel", rsSms.getString( "channel" ) );
            map.put( "priority", rsSms.getString( "priority" ) );
            map.put( "templateId", rsSms.getString( "template_id" ) );
        } else {
            throw new AfsBaseException( "短信模板未维护" );
        }
        return map;
    }

}
